﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using PSE.ProcessManager.Server.Process.Security;
using Sinacor.Infra.Core.ProcessManager.Server.Security;
using System.Data.Common;
using System.Data;

namespace Sinacor.Process.Bovespa.Negocio.Server.ResourceAccess
{
    public class ImportacaoExportacao : BaseResourceAccess
    {
        #region Construtor

        public ImportacaoExportacao()
            : base(AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.CompanyId)
		{
		}

		#endregion

        #region Métodos Públicos

                /// <summary>
        /// Obtem o codigo da empresa na bolsa
        /// </summary>
        public int ObterCodigoEmpresaBolsa()
        {
            int retorno;

            string sqlCommand = @"SELECT TO_NUMBER(CD_EMPR_BOLSA)
                                  FROM TGELOCAL
                                  WHERE CD_EMPRESA = :CompanyId
                                        AND CD_BOLSA = 1 ";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "CompanyId", DbType.Int32, AuthenticationContext<SinacorAuthenticationData>.Current.AuthenticationData.CompanyId);

                retorno = Convert.ToInt32(this.DataBase.ExecuteScalar(command));
            }

            return retorno;
        }

        /// <summary>
        /// Obtem o codigo da empresa na bolsa conforme o correspondente
        /// </summary>
        /// <param name="entity"></param>
        public int ObterCodigoEmpresaBolsaCorrespondente(ProcessEntity.Parameter.FiltroImportacaoNegocio entity)
        {
            int retorno;

            string sqlCommand = @"SELECT CD_MEMBRO_BOLSA
                                  FROM TBOCORRESP
                                  WHERE CD_CORRESP = :CodigoCorrespondente
                                        AND NVL(IN_BROKER,'N') = 'N' ";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "CodigoCorrespondente", DbType.Int32, entity.CodigoCorrespondente.Value);

                retorno = Convert.ToInt32(this.DataBase.ExecuteScalar(command));
            }

            return retorno;
        }

        #endregion
    }
}
